<template>
  <div>
    <input type="text" :value="value" style="display: none" />
    <el-select
      v-model="selected"
      style="width: 100%"
      :clearable="clearable"
      :size="size"
      :placeholder="placeholder"
      filterable
      default-first-option
      @change="handleItemChange"
    >
      <el-option
        v-for="item in orgList"
        :key="item.bank_code"
        :label="item.bank_name"
        :value="item.bank_code"
      ></el-option>
    </el-select>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    value: {
      type: String,
      default: '',
    },
    size: {
      type: String,
      default: '',
    },
    placeholder: {
      type: String,
      default: '选择银行名称',
    },
    clearable: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      selected: null,
      orgList: [],
      autoChange: true,
    }
  },
  computed: {},
  watch: {
    value(val) {
      this.selected = val
    },
  },
  created() {},
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      this.$http
        .get('/api/credit/bank_enum')
        .then(res => {
          this.orgList = res.data
          this.selected = this.value
        })
        .finally(() => {
          this.autoChange = false
        })
    },
    handleItemChange(val) {
      if (!this.autoChange) {
        this.$emit('input', val)
        let name = ''
        const i = this.orgList.findIndex(it => it.bank_code === val)
        if (i > -1) {
          name = this.orgList[i].bank_name
        }
        this.$emit('change', name)
        this.$emit('getname', name)
      }
    },
    init(val) {
      this.selected = val
    },
    getSelected() {
      return this.selected
    },
  },
}
</script>

<style scoped></style>
